﻿#include "./CameraFactory.h"
#include "CameraVisionKit.h"

#include <VkApiDefination.h>

namespace Qly{

QList<IndustryCameraInfo> Qly::CameraFactory::availableCameras(QSet<CameraType> types)
{
    QList<Qly::IndustryCameraInfo> infos;
    unsigned int device_type = 0;
    if( types.contains(CameraType::VISIONKIT_GIGE) )
    {
        device_type = DeviceTypeGigE;
    }
    if( types.contains(CameraType::VISIONKIT_U3V) )
    {
        device_type |= DeviceTypeU3V;
    }
    return findCameras(device_type);
}

QList<IndustryCameraInfo> Qly::CameraFactory::availableCameras(CameraType type)
{
    QList<Qly::IndustryCameraInfo> infos;
    unsigned int device_type;
    switch(type)
    {
    case CameraType::VISIONKIT_GIGE:
        device_type = DeviceTypeGigE;
        break;
    case CameraType::VISIONKIT_U3V:
        device_type = DeviceTypeU3V;
        break;
    default:
        return infos;
        break;
    }

    return findCameras(device_type);
}

QList<IndustryCameraInfo> CameraFactory::availableCameras()
{
    unsigned int device_type = DeviceTypeGigE | DeviceTypeU3V;
    return findCameras(device_type);
}



IndustryCamera *CameraFactory::findCameraByName(QString name, QObject *parent)
{
    Qly::CameraVisionKit * camera = new Qly::CameraVisionKit(parent);
    if(!camera->openCameraByID(name))
    {
        delete camera;
        camera = nullptr;
        qDebug() << "No Luster Camera Find";
    }
    return camera;
}

IndustryCamera *CameraFactory::defaultCamera(QObject *parent)
{
    int nDeviceNum = VkEnumCameraByType  (DeviceTypeGigE | DeviceTypeU3V  ) ; //TODO 现阶段只支持 USB 和 Gige
    if(nDeviceNum == 0) return nullptr;
    char name[128];
    int len = VkGetCameraNameByIndex(0, name, 128); // 查找第一个相机的名字
    if(len == 0) return nullptr;     // TODO 这里可能是错误的

    CameraVisionKit * camera = new Qly::CameraVisionKit(parent);
    if(!camera->openDefaultCamera())
    {
        delete camera;
        camera = nullptr;
        qDebug() << "No Luster Camera Find";
    }
    return camera;
}

QString CameraFactory::vendorName()
{
    return QString("Luster");
}

QList<IndustryCameraInfo> CameraFactory::findCameras(unsigned int device_type)
{
    QList<Qly::IndustryCameraInfo> infos;
    int nDeviceNum = VkEnumCameraByType  (device_type ) ;

    if (nDeviceNum == 0)
    {
        qWarning("No Luster VisionKit camera found!");
        return infos;
    }

    for(unsigned int nDeviceIndex = 0; nDeviceIndex < nDeviceNum; nDeviceIndex++)
    {
        char name[128];
        int len = VkGetCameraNameByIndex(nDeviceIndex, name, 128);

        Qly::IndustryCameraInfo info;
        info.cameraID = QString::fromLocal8Bit(name);
        info.modelName = QString::fromLocal8Bit(name);
        info.vendorName = "Luster";

        CAMERA camera  = VkGetCameraByName(name);
        if( camera ) // 如果获取成功，则进一步读取相机信息
        {
            int type = VkGetCameraType(camera);
            switch (type )
            {
            case DeviceTypeGigE :
                info.type = Qly::VISIONKIT_GIGE;
                infos << info;
                break;
            case DeviceTypeU3V:
                info.type = Qly::VISIONKIT_U3V;
                infos << info;
                break;
            }
            VkCameraRelease(camera);
        }
    }
    return infos;
}

} //namespace Qly

Qly::CameraFactoryInterface *CreateInstance()
{
    const char * ver = VkGetVersion();
    qDebug() << "VisionKit Version : " << ver;
    Qly::CameraFactory * cameraFactory = new Qly::CameraFactory();
    return cameraFactory;
}

void Release(Qly::CameraFactoryInterface *p)
{
    delete p;
    VkDeviceRelease();
}


bool Qly::CameraFactory::release(IndustryCamera *camera)
{
    Qly::CameraVisionKit * pCamera = dynamic_cast<Qly::CameraVisionKit *> (camera); // 能够转换成功说明是这个dll new出来的对象。可以删除。
    if(pCamera)
    {
        delete pCamera;
        return true;
    }
    return false; //说明不能删除这个对象
}

